﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using Teigha.DatabaseServices;
using Teigha.Geometry;
using Teigha.Colors;
using Teigha.Export_Import;
using Teigha.GraphicsInterface;
using Teigha.GraphicsSystem;
using Teigha.Runtime;

namespace ExportToDWGTool
{
    class UtilityFunc
    {

        public static bool FindCADLayerByNM(Database pDB, string strName)
        {

            if (pDB == null) return false;

            try
            {
                TransactionManager pTMgr = pDB.TransactionManager;
                using (Transaction pTransac = pTMgr.StartTransaction())
                {
                    LayerTable pLayerTable = pTMgr.GetObject(pDB.LayerTableId, OpenMode.ForRead) as LayerTable;

                    SymbolTableEnumerator pIterator = pLayerTable.GetEnumerator();

                    while (pIterator.MoveNext())
                    {
                        ObjectId objId = pIterator.Current;
                        LayerTableRecord pLayer = pTMgr.GetObject(objId, OpenMode.ForRead) as LayerTableRecord;

                        string strLy = pLayer.Name;

                        if (strLy == strName)
                        {
                            pTransac.Commit();
                            return true;
                        }

                    }

                    pTransac.Commit();
                }
                return false;
            }
            catch (System.Exception ex)
            {
                return false;
            }

        }



        public static ObjectId GetBlkIdByName(Database pDb, string strName)
        {
            ObjectId Oid = ObjectId.Null;
            try
            {

                TransactionManager pTMgr = pDb.TransactionManager;
                using (Transaction pTransac = pTMgr.StartTransaction())
                {
                    BlockTable pBTable = pTMgr.GetObject(pDb.BlockTableId, OpenMode.ForRead) as BlockTable;

                    foreach (ObjectId id in pBTable)
                    {

                        BlockTableRecord pBlock = pTMgr.GetObject(id, OpenMode.ForRead) as BlockTableRecord;
                        string str = pBlock.Name;
                        if (pBlock.Name == strName)
                        {
                            Oid = id;
                            break;
                        }

                        // m_Dic.Add(str, id);
                    }

                    pTransac.Commit();
                }



            }
            catch (System.Exception ex)
            {

            }
            //  m_Dic.Add("9999", Oid);
            return Oid;
        }

        public static ObjectId GetTLBlkIdByName(Database pDb, string strLX, string strGJ)
        {
            ObjectId Oid = ObjectId.Null;
            string strgj = strGJ;//.Substring(0, strGJ.IndexOf("mm"));
            try
            {
                using (BlockTable blockTable = (BlockTable)pDb.BlockTableId.GetObject(OpenMode.ForRead))
                {
                    foreach (ObjectId id in blockTable)
                    {
                        using (BlockTableRecord pBlock = (BlockTableRecord)id.Open(OpenMode.ForRead))
                        {
                            string str = pBlock.Name;
                            if (str == strLX + strgj)
                            {
                                Oid = id;
                                break;
                            }
                        }
                    }
                }
            }
            catch (System.Exception ex)
            {

            }
            return Oid;
        }

    }
}
